thin.imports('/public/configure/conf.pwd.js');
thin.imports('/public/configure/conf.minfo.js');
thin.imports('/public/plug/FormCombox/FormCombox.js');
thin.imports('/public/plug/LoadEffect/LoadEffect.js');

thin.use(function() {
	// 頁面加載完畢
	thin.effect();

	checkLocation();

});

var jude_memberindex = false,
	jude_editpwd = false,
	jude_editmobile = false,
	__getdata_minfobase = {},
	__getdata_pwdupdate = {},
	__getdata_resetmobile = {};

// 監聽地址欄
window.onpopstate = function() {
	// 路由
	checkLocation();
};

// 路由
function checkLocation(hash) {
	if(hash) {
		window.location.hash = hash;
		return;
	}
	var hash = window.location.hash;
	hash = hash.replace('#', '');
	(typeof checkLocationCallback == 'function') && checkLocationCallback(hash);
}

// 路由回調
function checkLocationCallback(hash) {
	if(hash == 'member' || hash == '') {
		if(!jude_memberindex) {
			__getdata_minfobase['model'] = 0;
			factory.request('_minfo_base', function() {
				$('#member-template').show().siblings().hide();
			});
		} else {
			$('#member-template').show().siblings().hide();
		}
		$('.left-nav').find('li').removeClass('active');
	}

	if(hash == 'editpwd') {
		if(!jude_editpwd) {
			// 綁定修改密碼按鈕
			$('#btn-editpwd-submit').on('click', submitEditpwd);
			createForm();
			jude_editpwd = true;
		} else {
			$('#editpwd-template').show().siblings().hide();
		}
		$('#nav-mem-xgmm').addClass('active').siblings().removeClass('active');
	}

	if(hash == 'editmobile') {
		if(!jude_editmobile) {
			// 綁定修改手机号按鈕
			$('#btn-editmobile-submit').on('click', submitEditMobile);
			layer.load(0, {
				shade: [0.08, '#000']
			});
			thin.imports('/public/webfe/resource/sms.js', function() {
				layer.closeAll('loading');
			});
			jude_editmobile = true;
		}
		$('#editmobile-template').show().siblings().hide();
		$('#nav-mem-xgsj').addClass('active').siblings().removeClass('active');
	}

	if(hash == 'account' || hash == 'member') {
		layer.load(0, {
			shade: [0.08, '#000']
		});
		// 获取账户余额
		factory.request('_m_acc');
		if (hash == 'account') {
			$('#account-template').show().siblings().hide();
			$('#nav-mem-wdqb').addClass('active').siblings().removeClass('active');
		}
	}
}

// 讀取個人信息回調
function __complete_minfobase(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 200) {
		var role;
		if(res.data.role == 1) {
			role = '创业者';
		} else if(res.data.role == 2) {
			role = '个人天使投资者';
		} else if(res.data.role == 3) {
			role = '机构投资者';
		}
		$('#nicknane').html(res.data.info.nickname);
		$('#mbinfo').html(res.data.info.city + '  ' + role + '<br />' + res.data.info.company);
		unkn();
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 渲染表單
function createForm() {
	var form = new FormCombox();
	form.addCombox([{
		'type': 'password',
		'label': '原密码',
		'name': 'passport',
		'placeholder': '请输入原密码'
	}, {
		'type': 'password',
		'label': '新密码',
		'name': 'password',
		'placeholder': '请输入新密码'
	}, {
		'type': 'password',
		'label': '再次输入',
		'name': 'confpwd',
		'placeholder': '再次输入新密码'
	}]);
	form.reader('layout-form-editpwd');
	$('#editpwd-template').show().siblings().hide();
}

// 修改手机号
function submitEditMobile() {
	var data_opt = {
		'mobile': $('#ipt-rm-mobile').val(),
		'new_mobile': $('[layout-mark=sms-mobile]').val(),
		'code': $('#ipt-rm-code').val()
	};
	if(data_opt.mobile == '') {
		layer.msg('请输入旧手机号', {
			icon: 2,
			time: 800
		});
		return;
	} else if(data_opt.new_mobile == '') {
		layer.msg('请输入新手机号', {
			icon: 2,
			time: 800
		});
		return;
	} else if(data_opt['code'] == '') {
		layer.msg('请输入验证码', {
			icon: 2,
			time: 800
		});
		return;
	}
	layer.load(0, {
		shade: [0.08, '#000']
	});
	__getdata_resetmobile['mobile'] = data_opt.mobile;
	__getdata_resetmobile['new_mobile'] = data_opt.new_mobile;
	verifySMSCode(function() {
		factory.request('_member_resetmobile');
	});
}

// 修改手机号回调
function __complete_resetmobile(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		layer.msg('修改成功', {
			icon: 1,
			time: 1200
		}, function() {
			window.location.href = '/member.html#member';
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 修改密碼
function submitEditpwd() {
	var data_opt = {};
	$('#layout-form-editpwd').find('input').each(function() {
		var name = this.name;
		data_opt[name] = this.value;
	});
	if(data_opt['passport'] == '') {
		layer.msg('请输入原密码', {
			time: 1200
		});
		return;
	} else if(data_opt['password'] == '') {
		layer.msg('请输入新密码', {
			time: 1200
		});
		return;
	} else if(data_opt['confpwd'] != data_opt['password']) {
		layer.msg('两次输入密码不正确', {
			time: 1200
		});
		return;
	}

	layer.load(0, {
		shade: [0.08, '#000']
	});

	__getdata_pwdupdate = data_opt;
	factory.request('_member_pwdupdate');
}

// 修改密碼回調
function __complete_pwdupdate(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		layer.msg('修改成功', {
			icon: 1,
			time: 1200
		}, function() {
			window.location.href = '/member.html#member';
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 獲取賬戶餘額回調
function __complete_macc(res) {
	layer.closeAll('loading');
	if(res.status == 200) {
		$('#acc-price').text(res.data);
		$('#bind-wallet').text(res.data);
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}